package com.account.boot.admin.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.account.boot.commons.exception.APIException;
import com.account.boot.commons.exception.APIExceptionUtils;
import com.account.boot.commons.result.APIResultDO;
import com.account.boot.commons.result.ResultCodeEnum;
import com.account.boot.commons.result.ResultDO;
import com.account.boot.interfaces.admin.AdminService;
import com.account.boot.model.vo.AdminVo;

@RestController
@RequestMapping("/")
public class LoginController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private AdminService adminService;
	
	/**
	 * 登录
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping("login")
	public Object login(String name, String password) {
		APIResultDO apiResultDO = APIResultDO.buildSuccessResult();
		try {
			if(StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
				throw new APIException(ResultCodeEnum.ADMIN_NAME_OR_PASSWORD_EMPTY);
			}
			ResultDO<AdminVo> resultDO = adminService.login(name, password);
			if(APIResultDO.CODE_SUCCESS != resultDO.getResultCode()) {
				throw new APIException(resultDO.getResultCode(), resultDO.getErrorMesssage());
			}
			apiResultDO.setData(resultDO.getModule());
		} catch (Exception e) {
			APIExceptionUtils.processException(apiResultDO, e, logger);
		}
		return apiResultDO;
	}
}
